<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Api extends Controller
{
    /**
     * @return Response_Response
     */
    private function getResponse()
    {
        return new Response_XmlResponse();
    }

    public function action_parse3()
    {
        $pi = ORM::factory('ParseInfo', 1);

        $dl = new Model_Loader_IteratorLoader($pi);

        $resp = $dl->load();
        $this->response->body($resp);
    }

    public function action_find()
    {
        $response = $this->getResponse();
        $this->response->headers('Content-Type', $response->getContentType());

        $logger = new Log_RequestLogger(DOCROOT.'logs/request');
        $logger->write();

        $limit = isset($_REQUEST['limit'])? $_REQUEST['limit'] : 1;
        $q = DB::select('id', 'parse_info_id', 'external_id', 'published_at', 'title', 'content', 'tags', 'updated_at')
                ->from('message')
                ->order_by('id', 'DESC')
                ->limit($limit);

        if( isset($_REQUEST['parse_info_id']) ) $q->and_where('parse_info_id', '=', $_REQUEST['parse_info_id']);
        if( isset($_REQUEST['tag']) ) $q->and_where('tags', 'LIKE', '%'.$_REQUEST['tag'].'%');

        $messages = $q->execute()->as_array();

        $this->response->body($response->getMessages($messages));
    }
}